﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.BBS
{
    public class ReplyAdd:TopicBasePage
    {
        public int MaxFileSize = 1000;
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected ReplyInfo ReplyData;
        protected int quote;

        protected new void HandleCommon()
        {

            this.AddLocation("回复帖子", "/BBS/ReplyAdd.aspx");

            if (UserData.Id <= 0)
            {
                this.AddErrLine("您还没有登录，请点击先登录！");
                this.MsgboxUrl = this.GetUrl("/User/Login.aspx");
                return;
            }
            UserInfo _User = UserBLL.Instance.GetUserInfo(UserData.Id);
            if ((_User.UserTypeId & 0x100)!=0)
            {
                this.AddErrLine("您已被在本站禁止发言");
                return;
            }

            quote = RequestTools.GetQueryInt("quote", 0);
            int rid = RequestTools.GetInt("rid", 0);

            if (rid > 0)
            {
                ReplyData = ReplyBLL.Instance.GetReplyInfo(rid);
            }
            else
            {
                ReplyData = new ReplyInfo();            //表明是对主题的回复
            }

      
        }

        protected void HandlePost()
        {
            ReplyInfo info = ValidateForm();
            if (info == null)
            {
                return;
            }
            string attachedFileAddressName = null;
            if (!string.IsNullOrEmpty(info.ReplyFile))
            {
                attachedFileAddressName = Utils.SaveRequestTopicFile(UserData.Id, MaxFileSize);
                if (attachedFileAddressName.Equals(""))
                {
                    AddErrLine("上传文件失败");
                    return;
                }
            }
            info.ReplyFile = attachedFileAddressName;
            if (ErrorCount > 0) return;
            bool succ = ReplyBLL.Instance.AddReply(info);
            if (!succ)
            {
                this.AddErrLine("操作失败！");
                return;
            }
            else
            {
                //更新帖子的更新时间
                TopicData.UpdateTime = DateTime.Now;
                TopicBLL.Instance.UpdateTopic(TopicData);
                UserBLL.Instance.UpdateUserCreditsByReply(UserData.Id);//设置用户积分
                this.Redirect(string.Format("/BBS/Topic.aspx?gid={0}&tid={1}", GroupData.Id, TopicData.Id));
            }
        }

        protected void HandleGet()
        {

        }

        protected ReplyInfo ValidateForm()
        {
            ReplyInfo data = new ReplyInfo();
            if (HttpContext.Current.Request.Files.Count > 0)
            {
                data.ReplyFile = HttpContext.Current.Request.Files[0].FileName;
            }

            if (!string.IsNullOrEmpty(data.ReplyFile))
            {
                string[] fileNames = data.ReplyFile.Split('.');
                string fileEXT = fileNames[fileNames.Length - 1];

                string fileRealNamePrefix = fileNames[fileNames.Length - 2];

                data.ReplyFileName = fileRealNamePrefix + "." + fileEXT;
            }
            data.Body = RequestTools.GetFormString("rbody").Trim();
            if (data.Body.Length <= 0)
            {
                this.AddErrLine("回复长度不能为空");
                return null;
            }
            else if (data.Body.Length > Config.ReplyMaxSize)
            {
                this.AddErrLine("回复长度不能大于 " + Config.ReplyMaxSize+"字符！");
                return null;
            }
            if (quote != 1)
            {
                data.ReplyFloorNo = ReplyData.FloorNo;
            }
            data.PostUserId = this.UserData.Id;
            data.PostUserName = this.UserData.UserName;
            data.TopicId = TopicData.Id;
            data.PostTime = DateTime.Now;
            data.UpdateTime = DateTime.Now;
            data.GroupId = GroupData.Id;
            data.FloorNo = TopicData.ReplyCount + 1;

            return data;
        }
    }
}